package org.jcms.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jcms.model.Admin;
import org.jcms.model.Customer;
import org.jcms.model.Manager;
import org.jcms.service.AdminServiceI;
import org.jcms.service.CustomerServiceI;
import org.jcms.service.ManagerServiceI;
import org.jcms.uitils.MD5;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.sun.xml.internal.fastinfoset.algorithm.IntEncodingAlgorithm;

/**
 * 改变角色的密码或停用状态
 * 
 * ID:用户名
 * 
 * 
 * type 1：学生、教师 2或3:教材发行/仓库采购 4：管理员
 * 
 * 
 * block=1 锁住帐号 block=0解锁帐号
 * 
 * resetpd= 重设密码
 * 
 * @author zzy
 * 
 */
public class AdminRolesChangeInfo extends HttpServlet {

	public AdminRolesChangeInfo() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		ApplicationContext ac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(this.getServletContext());
		AdminServiceI adminServiceI = (AdminServiceI) ac
				.getBean("adminServiceImpl");
		CustomerServiceI customerServiceI = (CustomerServiceI) ac
				.getBean("customerServiceImpl");
		ManagerServiceI managerServiceI = (ManagerServiceI) ac
				.getBean("managerServiceImpl");

		String retJson = "{\"error\":@err,\"msg\":\"@msg\"}";
		String err = "1";
		String msg = "";

		String ID = request.getParameter("ID");
		String type = request.getParameter("type");
		String block = request.getParameter("block");
		String resetpd = request.getParameter("resetpd");

		try {

			int Itype = Integer.parseInt(type);

			switch (Itype) {
			case 1:
				Customer customer = customerServiceI.findById(ID);

				if (customer == null) {
					msg = "该用户不存在";
					return;
				}
				if (block != null) {
					if (block.equals("1")) {
						customer.setCustomerIsBlocked(1);
						msg = "帐号" + ID + "停用成功!";
					} else if (block.equals("0")) {
						customer.setCustomerIsBlocked(0);
						msg = "帐号" + ID + "开启成功!";
					}
				}

				if (resetpd != null) {
					customer.setCustomerPassword(MD5.md5s(resetpd));
					msg += ("帐号" + ID + "密码重置成功!新密码" + resetpd);
				}
				customerServiceI.update(customer);
				err = "0";
				break;

			case 2:
			case 3:

				Manager manager = managerServiceI.findById(ID);
				if (manager == null) {
					msg = "该用户不存在";
					return;
				}
				if (block != null) {
					if (block.equals("1")) {
						manager.setManagerIsBlocked(1);
						msg = "帐号" + ID + "停用成功!";
					} else if (block.equals("0")) {
						manager.setManagerIsBlocked(0);
						msg = "帐号" + ID + "开启成功!";
					}
				}

				if (resetpd != null) {
					manager.setManagerPassword(MD5.md5s(resetpd));
					msg += ("帐号" + ID + "密码重置成功!新密码" + resetpd);
				}
				managerServiceI.update(manager);
				err = "0";
				break;

			case 4:
				Admin admin = adminServiceI.findById(ID);
				if (admin == null) {
					msg = "该用户不存在";
					return;
				}
				if (block != null) {
					if (block.equals("1")) {
						admin.setAdminIsBlocked(1);
						msg = "帐号" + ID + "停用成功!";
					} else if (block.equals("0")) {
						admin.setAdminIsBlocked(0);
						msg = "帐号" + ID + "开启成功!";
					}
				}

				if (resetpd != null) {
					admin.setAdminPassword(MD5.md5s(resetpd));
					msg += ("帐号" + ID + "密码重置成功!新密码" + resetpd);
				}
				adminServiceI.update(admin);
				err = "0";
				break;

			default:
				msg = "type错误";
				break;
			}
		}

		catch (Exception e) {
			e.printStackTrace();
			msg = "系统出错";
		}

		finally {
			retJson = retJson.replace("@err", err);
			retJson = retJson.replace("@msg", msg);
			response.getOutputStream().write(retJson.getBytes("utf-8"));
		}

	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
